package com.csdj.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {
    public static void main(String[] args) {
        try {
            //创建服务端的socket对象
            DatagramSocket serverSocket = new DatagramSocket(12345);
            byte [] bt = new byte[1024];
            //准备接收数据包
            DatagramPacket datagramPacket = new DatagramPacket(bt,bt.length);
            //接收数据
            serverSocket.receive(datagramPacket);
            //转换成字符串类
            String reply = new String(datagramPacket.getData(),
                    0,datagramPacket.getLength());
            System.out.println("客户端信息为：" + reply);
            serverSocket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
